<template>
    <div>
        <!--        党建资料管理-->
        <!--        头查询 -->
        <el-row :gutter="4">
            <el-col :span="4">
                <el-input v-model.trim="keyword.roomName" prefix-icon="fa fa-search" placeholder="文档名称"
                          @keyup.enter.native="initPolicyDocment"></el-input>
            </el-col>

            <el-col :span="2">
                <el-button type="primary" icon="fa fa-search" @click="initListInfo"
                           v-permission="'policy:document:list'"> 搜 索
                </el-button>
            </el-col>
            <el-col :span="2">
                <el-button type="primary" icon="fa fa-plus" @click="handleAdd"
                           v-permission="'policy:document:add'"> 添 加 信 息
                </el-button>
            </el-col>
            <el-col :span="2">
                <el-button type="danger" icon="fa fa-trash" @click="handleMultipleDelete"
                           v-permission="'policy:document:delete'"> 删
                    除 多 个
                </el-button>
            </el-col>
            <el-col :span="1" style="margin-left: 3px;">
                <el-button type="warning" icon="fa fa-refresh"
                           @click="initListInfo">
                </el-button>
            </el-col>
        </el-row>

        <el-row>
            <el-col :span="24">
                <el-table width="100%" border :data="page.row" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="50" align="center"></el-table-column>
                    <el-table-column prop="roomCampus" label="所属校区" align="center"></el-table-column>
                    <el-table-column prop="roomFloor" label="所属楼栋" align="center"></el-table-column>
                    <el-table-column prop="roomName" label="机房名称" align="center"></el-table-column>
                    <el-table-column prop="roomStartDate" label="建成时间" align="center"></el-table-column>
                    <el-table-column prop="roomBrand" label="品牌" align="center"></el-table-column>
                    <el-table-column prop="roomCount" label="机器数量" align="center"></el-table-column>
                    <el-table-column prop="roomUseableCount" label="可用数量" align="center"></el-table-column>
                    <el-table-column prop="roomHaveDisk" label="是否有盘" align="center"></el-table-column>
                    <el-table-column prop="roomDeskCount" label="桌椅数量" align="center"></el-table-column>
                    <el-table-column prop="roomMoney" label="建设金额" align="center"></el-table-column>
                    <el-table-column prop="roomCatagory" label="机房类型" align="center"></el-table-column>
                    <el-table-column prop="roomSwitchBrand" label="交换机品牌" align="center"></el-table-column>
                    <el-table-column prop="roomRouterBrand" label="路由器品牌" align="center"></el-table-column>
                    <el-table-column prop="roomRouterCount" label="路由器数量" align="center"></el-table-column>
                    <el-table-column prop="roomSpeaker" label="音响" align="center"></el-table-column>
                    <el-table-column prop="roomPhone" label="话筒" align="center"></el-table-column>
                    <el-table-column prop="roomFilesPath" label="相关附件" align="center">
                        <template slot-scope="scope">
                            <el-link v-if="scope.row.roomFilesPath" type="primary"
                                     :href="`${baseURL}${fileViewPath}${scope.row.roomFilesPath}`"
                                     target="_blank">
                                <i class="fa fa-search" aria-hidden="true"></i>
                                {{ scope.row.roomFilesPath | filterFileName }}
                            </el-link>
                        </template>
                    </el-table-column>
                    <el-table-column prop="roomDescription" label="备注" align="center"></el-table-column>

                    <el-table-column prop="contractName" label="机房合同名称" align="center" width="auto" ></el-table-column>
                    <el-table-column prop="contractFilePath" label="合同文书路径" align="center" width="auto">
                        <template slot-scope="scope">
                            <el-link v-if="scope.row.contractFilePath" type="primary"
                                     :href="`${baseURL}${fileViewPath}${scope.row.contractFilePath}`"
                                     target="_blank">
                                <i class="fa fa-search" aria-hidden="true"></i>
                                {{ scope.row.contractFilePath | filterFileName }}
                            </el-link>
                        </template>
                    </el-table-column>
                    <el-table-column prop="contractDescription" label="合同备注" align="center" width="auto"></el-table-column>

                    <el-table-column fixed="right" label="操作" width="200">
                        <template slot-scope="scope">
                            <el-button @click="handleEdit(scope.row)" icon="fa fa-pencil"
                                       v-permission="'policy:document:update'"> 修 改
                            </el-button>
                            <el-button type="danger" @click="handleDelete(scope.row)" icon="fa fa-times-rectangle"
                                       v-permission="'policy:document:delete'"> 删 除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>

        <!--        分页 -->
        <el-row>
            <el-col :span="22">
                <el-pagination v-if="page.total > 10"
                               @size-change="handleSizeChange"
                               @current-change="handleCurrentChange"
                               :current-page="pagination.current"
                               :page-sizes="[9 ,10, 20, 30, 50]"
                               :page-size="pagination.size"
                               layout="total, sizes, prev, pager, next"
                               :total="page.total">
                </el-pagination>
            </el-col>
        </el-row>

        <el-dialog
                v-dialog-drag
                :title="isAdd ? '新 增 数 据' : '修 改 数 据'"
                :visible.sync="centerDialogVisible"
                width="40%"
                center
                @close="resetField"
        >


            <el-form ref="form" :rules="rules" :model="data" label-width="80px">

                <el-form-item label="所属校区:" prop="roomCampus">
                    <el-select v-model="data.roomCampus" placeholder="选择校区">
                        <el-option
                                v-for="item in roomCampusList"
                                :key="item"
                                :label="item"
                                :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>


                <el-form-item label="所属楼栋" label-width="75px">
                    <el-input v-model="data.roomFloor"></el-input>
                </el-form-item>
                <el-form-item label="机房名称" label-width="75px">
                    <el-input v-model="data.roomName"></el-input>
                </el-form-item>

                <el-form-item label="建成时间" label-width="75px" prop="roomStartDate">
                    <el-date-picker
                            v-model="data.roomStartDate"
                            type="date"
                            format="yyyy-MM-dd"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="品牌" label-width="75px">
                    <el-input v-model="data.roomBrand"></el-input>
                </el-form-item>
                <el-form-item label="机器数量" label-width="75px">
                    <el-input v-model="data.roomCount"></el-input>
                </el-form-item>

                <el-form-item label="可用数量" label-width="75px">
                    <el-input v-model="data.roomUseableCount"></el-input>
                </el-form-item>

                <el-form-item label="是否有盘:" prop="roomHaveDisk">
                    <el-select v-model="data.roomHaveDisk" placeholder="是否有盘" >
                        <el-option
                                v-for="item in roomHaveDiskList"
                                :key="item"
                                :label="item"
                                :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="桌椅数量" label-width="75px">
                    <el-input v-model="data.roomDeskCount"></el-input>
                </el-form-item>
                <el-form-item label="建设金额" label-width="75px">
                    <el-input v-model="data.roomMoney"></el-input>
                </el-form-item>

                <el-form-item label="机房类型:" prop="roomCatagory">
                    <el-select v-model="data.roomCatagory" placeholder="选择机房类型" >
                        <el-option
                                v-for="item in roomCatagoryList"
                                :key="item"
                                :label="item"
                                :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="交换机品牌" label-width="75px">
                    <el-input v-model="data.roomSwitchBrand"></el-input>
                </el-form-item>
                <el-form-item label="路由器品牌" label-width="75px">
                    <el-input v-model="data.roomRouterBrand"></el-input>
                </el-form-item>
                <el-form-item label="路由器数量" label-width="75px">
                    <el-input v-model="data.roomRouterCount"></el-input>
                </el-form-item>
                <el-form-item label="音响" label-width="75px">
                    <el-input v-model="data.roomSpeaker"></el-input>
                </el-form-item>
                <el-form-item label="话筒" label-width="75px">
                    <el-input v-model="data.roomPhone"></el-input>
                </el-form-item>

                <el-form-item label="相关附件:" label-width="80px">
                    <el-upload
                            ref="upload"
                            drag
                            :headers="fileHeaders"
                            :action="baseURL.concat('file/upload')"
                            name="file"
                            :data="fileDir"
                            :on-success="handleFileUploadSuccess"
                            :on-error="handleFileUploadError"
                            :show-file-list=false
                    >
                        <i class="el-icon-upload"></i>
                        <div class="el-upload__text"> 拖 拽 或 <em>点 击 上 传 </em></div>
                        <div class="el-upload__tip" slot="tip">建议上传之后浏览 默认不解析 utf-8 以外字符</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="检查上传文件:" label-width="120px">
                    <el-link v-if="data.roomFilesPath" :href="`${baseURL}${fileViewPath}${data.roomFilesPath}`"
                             target="_blank">
                        {{ data.roomFilesPath | filterFileName }} <i
                            class="el-icon-view el-icon--right"></i>
                    </el-link>
                    <el-tag v-else type="success"> 无 上 传 文 件</el-tag>
                </el-form-item>

                <el-form-item label="备注" label-width="75px">
                    <el-input v-model="data.roomDescription"></el-input>
                </el-form-item>




            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="centerDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addDocumentInfo">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    import {authKey, baseURL, fileViewPath} from "@/config/constant";
    import {addDocument, page,updateInfo,removeFile,deleteDocument,deleteMultipleDocument} from "@/api/modules/room/base/index";
    import {mapState} from "vuex";
    import {form, recommendTeacher} from "@/api/modules/dictionary";

    export default {
        name: "RoomBase",
        data(){
            return {
                baseURL, fileViewPath,
                keyword: {
                    "roomName": "",
                },
                page: {
                    row: [],
                    total: 0
                },
                pagination: {
                    current: 1,
                    size: 9
                },

                // 推荐信息
                recommend: {
                    teacher: [],
                    form: {}
                },

                roomCampusList:[
                    "河东校区",
                    "龙凤校区",
                    "锂电学院",
                    "汽车学院",
                    "其他",
                ],
                roomCatagoryList:[
                    "软件",
                    "网络",
                    "动漫",
                    "其他",
                ],
                roomHaveDiskList:[
                    "有盘","无盘"
                ],
                multipleSelection: [],
                data: {
                    "id": "",
                    "roomCampus": "",
                    "roomFloor": "",
                    "roomName": "",
                    "roomStartDate": "",
                    "roomBrand": "",
                    "roomCount": "",
                    "roomUseableCount": "",
                    "roomHaveDisk": "",
                    "roomDeskCount": "",
                    "roomMoney": "",
                    "roomCatagory": "",
                    "roomSwitchBrand": "",
                    "roomRouterBrand": "",
                    "roomRouterCount": "",
                    "roomSpeaker": "",
                    "roomPhone": "",
                    "roomFilesPath": "",
                    "roomDescription": "",

                    //机房建设方案
                    "contractName":"",
                    "contractFilePath":"",
                    "contractDescription":"",

                },
                isAdd: true,
                centerDialogVisible: false,
                rules: {
                    documentionName: [{required: true, message: '请输入名称', trigger: 'blur'}]
                },
                documentionCatagory:[
                    "党建上交材料","党员活动材料","三会一课材料","党组织活动材料","工作计划管理","工作总结","其他材料"
                ]
            }
        },
        filters: {
            filterFileName(value) {
                // 斜杠位置
                let slashIndex = value.lastIndexOf("/");
                // 文件随机数位置
                let randomIndex = value.indexOf("-");
                return value.slice(slashIndex + 1, randomIndex) + value.slice(value.indexOf('.'));
            }
        },
        methods:{

            //选择结果
            handleSelect(item){
                this.data.teacherId=item.id;
            },
            initRecommend() {
                recommendTeacher().then(response => {
                    this.recommend.teacher = response;
                });
                form().then(response => {
                    this.recommend.form = response;
                });
            },
            initListInfo(){
                page(this.pagination, this.keyword).then(response => {
                    this.page.row = response.row;
                    this.page.total = response.total;
                })
            },

            // 查询
            querySearch(queryString, cb) {
                cb(this.recommend.teacher.filter(item => item.value.indexOf(queryString) !== -1))
            },

            // 删除单个
            handleDelete(row) {
                this.$confirm(`此操作将永久删除 <strong><i> ${row.documentionName} </i></strong> 是否继续?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    dangerouslyUseHTMLString: true,
                    type: 'warning'
                }).then(() => {
                    deleteDocument(row.id).then(() => {
                        // 刷新页面
                        this.initListInfo();
                    });
                })

            },
            // 删除多个
            handleMultipleDelete() {
                // 大于 0 才删除
                if (this.multipleSelection.length > 0) {
                    this.$confirm(`此操作将永久删除<strong> 多条信息</strong>是否继续?`, '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        dangerouslyUseHTMLString: true,
                        type: 'warning'
                    }).then(() => {
                        deleteMultipleDocument(this.multipleSelection).then(() => {
                            // 刷新页面
                            this.initListInfo();
                        });
                    })
                }
            },

            // 修改单个
            handleEdit(row) {
                // 不是添加
                this.isAdd = false;
                Object.assign(this.data, row);
                this.centerDialogVisible = true;
            },

            // 多选框
            handleSelectionChange(row) {
                // 获取 ids
                this.multipleSelection = row.map(item => item.id);
            },
            // 当前页改变
            handleCurrentChange(current) {
                this.pagination.current = current;
                this.initListInfo();
            },

            // 当前页大小改变
            handleSizeChange(size) {
                this.pagination.size = size;
                this.initListInfo();
            },

            addDocumentInfo(){
                this.$refs.form.validate(valid => {
                    if (valid) {
                        // 是新增就进行添加
                        if (this.isAdd) {
                            addDocument(this.data).then(() => {
                                // TODO 添加信息 不需要 init
                                this.centerDialogVisible = false;
                                this.initListInfo();
                            })
                        } else {
                            updateInfo(this.data).then(() => {
                                // TODO 修改信息 不需要 init
                                this.centerDialogVisible = false;
                                this.initListInfo();
                            })
                        }
                    }
                })
            },

            handleAdd() {
                // 是添加
                this.isAdd = true;
                Object.assign(this.data, {
                    "id": "",
                    "teacherId": "",
                    "studentId": "-1",
                    "studyDate": "",
                    "studySubject": "",
                    "studyHost": "",
                    "studyDescription": "",
                    "linkUrl": "",

                    "teacherName": "",
                    "teacherGender": "",
                    "teacherDepartment": "",
                });
                this.centerDialogVisible = true;
            },
            resetField() {
                // 清空表单验证
                this.$refs.form.resetFields();
            },

            async handleFileUploadSuccess(response) {
                await removeFile(this.data.roomFilesPath);
                if (response.code <= 30000 && response.code >= 20000) {
                    this.data.roomFilesPath = response.data;
                    this.$message.success({message: response.msg})
                } else {
                    this.$message.error({message: response.msg})
                }
            },

            handleFileUploadError() {
                this.$message.error({message: "上传失败"})
            },
        },
        computed:{
            ...mapState("user", ["userToken"]),
            fileHeaders() {
                // 文件上传携带头信息
                let obj = {};
                obj[authKey] = this.userToken;
                return obj
            },
            fileDir() {
                // 上传的文件位置
                return {dir: "policy"}
            }
        },
        mounted() {
            this.initListInfo()
            this.initRecommend()
        }
    }
</script>

<style scoped>

</style>